// components/blog-ctrl/blog-ctrl.js
import regeneratorRuntime from '../../utils/runtime.js'
let userInfo = {}
const db = wx.cloud.database()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    blogId: String,
    blog: Object,
  },
  externalClasses: ['iconfont', 'icon-pinglun', 'icon-fenxiang'],

  /**
   * 组件的初始数据
   */
  data: {
    // 登录组件是否显示
    loginShow: false,
    // 底部弹出层是否显示
    modalShow: false,
    content: '',
  },

  /**
   * 组件的方法列表
   */
  
  methods: {
    onComment() {
      // 判断用户是否授权
      wx.getSetting({
        withSubscriptions: true,
        success: (res) => {
         
          if (res.authSetting['scope.userInfo']) {
           
            wx.getUserInfo({
              success: (res) => {
                userInfo = res.userInfo
                
                // 显示评论弹出层
                this.setData({
                  modalShow: true,
                })
              }
            })
          } else {
            this.setData({
              loginShow: true,
            })
            
          }
        }
      })
    },

    onLoginsuccess(event) {
      userInfo = event.detail
      // 授权框消失，评论框显示
      this.setData({
        loginShow: false,
      }, () => {
        this.setData({
          modalShow: true,
        })
      })
    },

    onLoginfail() {
      wx.showModal({
        title: '授权用户才能进行评价',
        content: '',
      })
    },
    async onSend(event) {
      await wx.requestSubscribeMessage({
        tmplIds: ['N8Rka1W4adwNiFqLZEdYqhR6CofF1TT_SV4FjuxEmjI'],
        success:(res)=> {
          console.log(res)
          console.log(event)
          // 插入数据库
          //识别是哪个表单，
          //1有有效期 2每次提交一个，生成一次id
          let formId = event.detail.formId
          let content = event.detail.value.content
          if (content.trim() == '') {
            //为空的话就不操作
            wx.showModal({
              title: '评论内容不能为空',
              content: '',
            })
            return
          }
          wx.showLoading({
            title: '评论中',
            mask: true,
          })
          //插入数据的话，在小程序端比较方便，可以直接加openid
          db.collection('blog-comment').add({
            data: {
              content,
              createTime: db.serverDate(),
              blogId: this.properties.blogId,
              nickName: userInfo.nickName,
              avatarUrl: userInfo.avatarUrl
            }
          }).then((res) => {
            // 推送模板消息
            wx.cloud.callFunction({
              name: 'sendMessage',
              data: {
                content,
                //formId,
                blogId: this.properties.blogId
              }
            }).then((res) => {
              console.log(res)
            })

            wx.hideLoading()
            wx.showToast({
              title: '评论成功',
            })
            this.setData({
              modalShow: false,
              content: '',
            })

            // 父元素刷新评论页面
            this.triggerEvent('refreshCommentList')
          })
        },
        fail(err) {
          console.log(err)
          return
        }
      })
   
    },

  }
})